package com.example.swd1.gudong.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Xfermode;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 * Created by user on 9/19/16.
 */
public abstract class MaskedImage extends ImageView {
    private Bitmap mask;
    private Paint paint;
    private Xfermode mXfermode=new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
    public MaskedImage(Context paramContext) {
        super(paramContext);
    }

    public MaskedImage(Context paramContext, AttributeSet paramAttributeSet) {
        super(paramContext, paramAttributeSet);
    }

    public MaskedImage(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {
        super(paramContext, paramAttributeSet, paramInt);
    }

    public abstract Bitmap createMask();

    @Override
    protected void onDraw(Canvas paramCanvas) {
        Drawable localDrawable = getDrawable();
        paint = new Paint();
        paint.setFilterBitmap(false);
        //显示交集下层
        paint.setXfermode(mXfermode);

        int width = getWidth();
        int height = getHeight();
        int i = paramCanvas.saveLayer(0.0F, 0.0F, width, height, null, Canvas.ALL_SAVE_FLAG);
        localDrawable.setBounds(0, 0, width, height);
        localDrawable.draw(paramCanvas);
        if ((this.mask == null) || (this.mask.isRecycled())) {
            this.mask = createMask();
        }
        paramCanvas.drawBitmap(mask, 0.0F, 0.0F, paint);
        paramCanvas.restoreToCount(i);
    }
}